layui.define(['form', 'laydate', 'xmSelect'], function (exports) {
    var $ = layui.$
        , form = layui.form
        , setter = layui.setter
        , laydate = layui.laydate;


    lay('#confirmArrivalTime').on('click', function (e) {
        laydate.render({
            elem: '#confirmArrivalTime'
            , type: "datetime"
            , format: "yyyy-MM-dd HH:mm"
            , trigger: 'click'
            , show: true //直接显示
            , closeStop: '#createTime' //这里代表的意思是：点击 test1 所在元素阻止关闭事件冒泡。如果不设定，则无法弹出控件
            ,done:(res)=>{
                if ($("#confirmArrivalTime").val() !== "" && $("#durationTime").val() !== "请选择") {
                    layui.admin.req({
                        url: setter.host + '/reservation/receiveManager/freePlatformByTime'
                        , type: 'post'
                        , contentType: "application/x-www-form-urlencoded"
                        , data: {
                            startTime: res,
                            keepTime: Number($("#durationTime").val())
                        }
                        , done: function (res) {
                            //请求成功后
                            if (res.code == 0) {
                                $(`[id=platform] option`).remove()
                                for (let r of res.data) {
                                    if (r.sure === 1) {
                                        $(`[id=platform]`).append(`<option value="${r.platformNumber}">${r.platformCode}</option>`)
                                    } else {
                                        $(`[id=platform]`).append(`<option disabled="disabled" value="${r.platformNumber}">${r.platformCode}</option>`)
                                    }
                                }
                                layui.form.render('select');
                            } else {
                                layer.msg(res.msg);
                            }
                        }
                    })


                    layui.admin.req({
                        url: setter.host + '/reservation/receiveManager/freeReceiverByTime'
                        , type: 'post'
                        , contentType: "application/x-www-form-urlencoded"
                        , data: {
                            startTime: res,
                            keepTime: Number($("#durationTime").val())
                        }
                        , done: function (res) {
                            $(`[id =receiver] option`).remove()
                            if (res.code == 0) {
                                for (let r of res.data) {
                                    if (r.sure === 1) {
                                        $(`[id=receiver]`).append(`<option value="${r.receiverNumber}">${r.receiverName}</option>`)
                                    } else {
                                        $(`[id=receiver]`).append(`<option disabled="disabled" value="${r.receiverNumber}">${r.receiverName}</option>`)
                                    }
                                }
                                layui.form.render('select');
                            } else {
                                layer.msg(res.msg);
                            }
                        }
                    })

                }
            }
        });
    });


    form.on('select(typeSelect)', function (data) {
        if (data.value == 'button') {
            $('#url').val('add');
            $('#menuName1').find("input").val('新增');
            $('#menuName1').css('display', 'none');
            $('#menuName2').css('display', 'block');
        } else {
            $('#url').val('');
            $('#menuName1').find("input").val('');
            $('#menuName1').css('display', 'block');
            $('#menuName2').css('display', 'none');
        }
    });
    form.on('select(buttonSelect)', function (data) {

        if (data.value == 'add') {
            $('#menuName1').find("input").val('新增');
            $('#url').val('add');
            $('input[name="name"]').val('add');
        } else if (data.value == 'edit') {
            $('#menuName1').find("input").val('编辑');
            $('#url').val('edit');
        } else if (data.value == 'batchdel') {
            $('#menuName1').find("input").val('删除');
            $('#url').val('batchdel');
        } else if (data.value == 'export') {
            $('#menuName1').find("input").val('导出');
            $('#url').val('export');
        }
    });


    form.on('select(durationSelect)', function (data) {

        //获取input输入的值
        if ($("#confirmArrivalTime").val() !== "" && $("#durationTime").val() !== "请选择") {
            layui.admin.req({
                url: setter.host + '/reservation/receiveManager/freePlatformByTime'
                , type: 'post'
                , contentType: "application/x-www-form-urlencoded"
                , data: {
                    startTime: $("#confirmArrivalTime").val(),
                    keepTime: Number(data.value)
                }
                , done: function (res) {
                    //请求成功后
                    if (res.code == 0) {
                        $(`[id=platform] option`).remove()
                        for (let r of res.data) {
                            if (r.sure === 1) {
                                $(`[id=platform]`).append(`<option value="${r.platformNumber}">${r.platformCode}</option>`)
                            } else {
                                $(`[id=platform]`).append(`<option disabled="disabled" value="${r.platformNumber}">${r.platformCode}</option>`)
                            }
                        }
                        layui.form.render('select');
                    } else {
                        layer.msg(res.msg);
                    }
                }
            })


            layui.admin.req({
                url: setter.host + '/reservation/receiveManager/freeReceiverByTime'
                , type: 'post'
                , contentType: "application/x-www-form-urlencoded"
                , data: {
                    startTime: $("#confirmArrivalTime").val(),
                    keepTime: Number(data.value)
                }
                , done: function (res) {
                    $(`[id =receiver] option`).remove()
                    if (res.code == 0) {
                        for (let r of res.data) {
                            if (r.sure === 1) {
                                $(`[id=receiver]`).append(`<option value="${r.receiverNumber}">${r.receiverName}</option>`)
                            } else {
                                $(`[id=receiver]`).append(`<option disabled="disabled" value="${r.receiverNumber}">${r.receiverName}</option>`)
                            }
                        }
                        layui.form.render('select');
                    } else {
                        layer.msg(res.msg);
                    }
                }
            })

        }

    })


    // form.on('select(receiver)', function (data) {
    //
    //     //获取input输入的值
    //     if ($("#confirmArrivalTime").val() !== "" && $("#durationTime").val() !== "请选择") {
    //         layui.admin.req({
    //             url: setter.host + '/reservation/receiveManager/freePlatformByTime'
    //             , type: 'post'
    //             , contentType: "application/x-www-form-urlencoded"
    //             , data: {
    //                 startTime: $("#confirmArrivalTime").val(),
    //                 keepTime: Number($("#durationTime").val())
    //             }
    //             , done: function (res) {
    //                 //请求成功后
    //                 if (res.code == 0) {
    //                     $(`[id=platform] option`).remove()
    //                     for (let r of res.data) {
    //                         if (r.sure === 1) {
    //                             $(`[id=platform]`).append(`<option value="${r.platformNumber}">${r.platformCode}</option>`)
    //                         } else {
    //                             $(`[id=platform]`).append(`<option disabled="disabled" value="${r.platformNumber}">${r.platformCode}</option>`)
    //                         }
    //                     }
    //                     layui.form.render('select');
    //                 } else {
    //                     layer.msg(res.msg);
    //                 }
    //             }
    //         })
    //
    //
    //         layui.admin.req({
    //             url: setter.host + '/reservation/receiveManager/freeReceiverByTime'
    //             , type: 'post'
    //             , contentType: "application/x-www-form-urlencoded"
    //             , data: {
    //                 startTime: $("#confirmArrivalTime").val(),
    //                 keepTime: Number($("#durationTime").val())
    //             }
    //             , done: function (res) {
    //                 $(`[id =receiver] option`).remove()
    //                 if (res.code == 0) {
    //                     for (let r of res.data) {
    //                         if (r.sure === 1) {
    //                             $(`[id=receiver]`).append(`<option value="${r.receiverNumber}">${r.receiverName}</option>`)
    //                         } else {
    //                             $(`[id=receiver]`).append(`<option disabled="disabled" value="${r.receiverNumber}">${r.receiverName}</option>`)
    //                         }
    //                     }
    //                     layui.form.render('select');
    //                 } else {
    //                     layer.msg(res.msg);
    //                 }
    //             }
    //         })
    //
    //     }
    //
    // })



    // form.on('select(platform)', function (data) {
    //
    //     //获取input输入的值
    //     if ($("#confirmArrivalTime").val() !== "" && $("#durationTime").val() !== "请选择") {
    //         layui.admin.req({
    //             url: setter.host + '/reservation/receiveManager/freePlatformByTime'
    //             , type: 'post'
    //             , contentType: "application/x-www-form-urlencoded"
    //             , data: {
    //                 startTime: $("#confirmArrivalTime").val(),
    //                 keepTime: Number($("#durationTime").val())
    //             }
    //             , done: function (res) {
    //                 //请求成功后
    //                 if (res.code == 0) {
    //                     $(`[id=platform] option`).remove()
    //                     for (let r of res.data) {
    //                         if (r.sure === 1) {
    //                             $(`[id=platform]`).append(`<option value="${r.platformNumber}">${r.platformCode}</option>`)
    //                         } else {
    //                             $(`[id=platform]`).append(`<option disabled="disabled" value="${r.platformNumber}">${r.platformCode}</option>`)
    //                         }
    //                     }
    //                     layui.form.render('select');
    //                 } else {
    //                     layer.msg(res.msg);
    //                 }
    //             }
    //         })
    //
    //
    //         layui.admin.req({
    //             url: setter.host + '/reservation/receiveManager/freeReceiverByTime'
    //             , type: 'post'
    //             , contentType: "application/x-www-form-urlencoded"
    //             , data: {
    //                 startTime: $("#confirmArrivalTime").val(),
    //                 keepTime: Number($("#durationTime").val())
    //             }
    //             , done: function (res) {
    //                 $(`[id =receiver] option`).remove()
    //                 if (res.code == 0) {
    //                     for (let r of res.data) {
    //                         if (r.sure === 1) {
    //                             $(`[id=receiver]`).append(`<option value="${r.receiverNumber}">${r.receiverName}</option>`)
    //                         } else {
    //                             $(`[id=receiver]`).append(`<option disabled="disabled" value="${r.receiverNumber}">${r.receiverName}</option>`)
    //                         }
    //                     }
    //                     layui.form.render('select');
    //                 } else {
    //                     layer.msg(res.msg);
    //                 }
    //             }
    //         })
    //
    //     }
    //
    // })

    //对外暴露的接口
    exports('confirm', {});
});